package casa.depesos.app.com.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import casa.depesos.app.com.App;
import casa.depesos.app.com.R;
import casa.depesos.app.com.databinding.OrderDetailItemBinding;
import casa.depesos.app.com.http.data.OrderDetail;
import casa.depesos.app.com.utils.Utils;


public class OrderDetailAdapter {

    OrderDetail order;
    LinearLayout linearLayout;
    LayoutInflater inflater;

    public OrderDetailAdapter(OrderDetail order, LinearLayout linear) {
        this.order = order;
        linearLayout = linear;
        inflater = LayoutInflater.from(linear.getContext());
    }

    public void fill() {
        linearLayout.removeAllViews();
        if (order == null || order.status == null) {
            return;
        }
        //申请
        OrderDetailItemBinding binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
        binding.icon.setImageResource(R.mipmap.small_right);
        binding.title.setText("Solicitar préstamo");
        binding.time.setText(Utils.getDay2(order.applyTime));

        switch (order.status) {
            case "10": {
                //审核中
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_blue_tag);
                binding.title.setText("Préstamo en revisión");
                binding.time.setText(Utils.getDay2(order.verifyingTime));
                //通过
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_gray_tag);
                binding.title.setText("Su préstamo ha sido aprobado, está desembolsando");
                binding.time.setText(Utils.getDay2(order.verityTime));
                //放款失败
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_red_tag);
                binding.title.setText("Desembolso fallido");
                binding.title.setTextColor(0xffE91C1C);
                binding.title.setTextSize(16);
                binding.time.setText(Utils.getDay2(order.failTime));
                binding.desc.setText("La información de su tarjeta bancaria es incorrecta, el desembolso falló,  verifique o cambie su tarjeta bancaria y vuelva a ingresar la cuenta para retiros");
                return;
            }
            case "20": {
                //审核中
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_blue_tag);
                binding.title.setText("Préstamo en revisión");
                binding.time.setText(Utils.getDay2(order.verifyingTime));
                //通过
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_gray_tag);
                binding.title.setText("Su préstamo ha sido aprobado, está desembolsando");
                binding.time.setText(Utils.getDay2(order.verityTime));
                binding.desc.setText("Su solicitud de retiro ha sido aprobada y se ha notificado al banco para el desembolso. El tiempo de llegada depende del banco (puede haber retrasos en días festivos), por favor sea paciente");
                return;
            }
            case "70": {
                //审核中
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_blue_tag);
                binding.title.setText("Préstamo en revisión");
                binding.time.setText(Utils.getDay2(order.verifyingTime));
                //审核失败
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_red_tag);
                binding.title.setText("Revisión de préstamo rechazada");
                binding.time.setText(Utils.getDay2(order.verityTime));
                binding.desc.setText("La transacción falló y temporalmente no podemos prestarle dinero. Intente solicitar un préstamo nuevamente después de que termine la cuenta regresiva");
                return;
            }
            case "30": {
                //审核中
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_blue_tag);
                binding.title.setText("Préstamo en revisión");
                binding.time.setText(Utils.getDay2(order.verifyingTime));
                //通过
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_gray_tag);
                binding.title.setText("Su préstamo ha sido aprobado, está desembolsando");
                binding.time.setText(Utils.getDay2(order.verityTime));
                if (!TextUtils.isEmpty(order.failTime)) {
                    //放款失败
                    binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                    binding.icon.setImageResource(R.drawable.small_red_tag);
                    binding.title.setText("Desembolso fallido");
                    binding.title.setTextColor(0xffE91C1C);
                    binding.title.setTextSize(16);
                    binding.time.setText(Utils.getDay2(order.failTime));
                }
                //放款成功
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_gray_tag);
                binding.title.setText("Desembolso exitoso");
                binding.time.setText(Utils.getDay2(order.lenderTime));
                //还款
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                if (order.isOverdue) {
                    binding.icon.setImageResource(R.drawable.small_red_tag);
                    binding.title.setTextColor(0xffE91C1C);
                    String s = String.format(App.getApp().getString(R.string.overday2), (Float.valueOf(order.overdueDay).intValue()) + "");
                    binding.title.setText(s);
                    binding.desc.setText("Por favor realice el pago a tiempo, el retraso afectará su crédito personal y deberá pagar tarifas por mora");
                } else {
                    binding.icon.setImageResource(R.drawable.small_gray_tag);
                    binding.title.setText("Pago pendiente");
                    binding.desc.setText("Su préstamo ha sido desembolsado con éxito, ¡por favor esté atento a su recepción!");
                }
                binding.time.setText(Utils.getDay2(order.lenderTime));
                return;
            }
            case "40": {
                //审核中
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_blue_tag);
                binding.title.setText("Préstamo en revisión");
                binding.time.setText(Utils.getDay2(order.verifyingTime));
                //通过
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_gray_tag);
                binding.title.setText("Su préstamo ha sido aprobado, está desembolsando");
                binding.time.setText(Utils.getDay2(order.verityTime));
                if (!TextUtils.isEmpty(order.failTime)) {
                    //放款失败
                    binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                    binding.icon.setImageResource(R.drawable.small_red_tag);
                    binding.title.setText("Desembolso fallido");
                    binding.title.setTextColor(0xffE91C1C);
                    binding.title.setTextSize(16);
                    binding.time.setText(Utils.getDay2(order.failTime));
                }
                //放款成功
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_gray_tag);
                binding.title.setText("Desembolso exitoso");
                binding.time.setText(Utils.getDay2(order.lenderTime));
                //还款
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                if (order.isOverdue) {
                    binding.icon.setImageResource(R.drawable.small_red_tag);
                    binding.title.setTextColor(0xffE91C1C);
                    String s = String.format(App.getApp().getString(R.string.overday2), (Float.valueOf(order.overdueDay).intValue()) + "");
                    binding.title.setText(s);
                } else {
                    binding.icon.setImageResource(R.drawable.small_gray_tag);
                    binding.title.setText("Pago pendiente");
                }
                binding.time.setText(Utils.getDay2(order.lenderTime));
                //还清
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_gray_tag);
                binding.title.setText("Liquidado");
                binding.time.setText(Utils.getDay2(order.settleTime));
                return;
            }
            case "60": {
                //审核中
                binding = OrderDetailItemBinding.inflate(inflater, linearLayout, true);
                binding.icon.setImageResource(R.drawable.small_blue_tag);
                binding.title.setText("Préstamo en revisión");
                binding.time.setText(Utils.getDay2(order.verifyingTime));
                binding.desc.setText("Su préstamo está siendo revisado, espere pacientemente");
                return;
            }
        }
    }

    public int getItemCount() {
        //10 放款失败  20 放款中  30 还款中 40 已结清 60审核中 70 审核失败
        if (order == null || order.status == null) {
            return 0;
        }
        switch (order.status) {
            case "10":
                return 4;
            case "20":
            case "70":
                return 3;
            case "30":
                return TextUtils.isEmpty(order.failTime) ? 5 : 6;
            case "40":
                return TextUtils.isEmpty(order.failTime) ? 6 : 7;
            case "60":
                return 2;
            default:
                return 0;
        }
    }
}
